package a.a;

import a.b.SectionCursorAdatper;
import a.db.MyDatabase;
import a.db.MyDatabase.SectionTest;
import a.db.MyDatabase.Test;
import android.app.ListActivity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView.RecyclerListener;
import android.widget.ListView;
import android.widget.TextView;

public class SectionCursorTutorialActivity extends ListActivity implements RecyclerListener {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

//		 for (int i = 0; i < 2; i++) {
//		 insertData();
//		 }

		new AsyncTask<Void, Void, Void>() {
			@Override
			protected Void doInBackground(Void... params) {
				return null;
			}

			protected void onPreExecute() {

			};

			protected void onPostExecute(Void result) {
				Cursor c = managedQuery(MyDatabase.Test.CONTENT_URI, null, null, null, Test.NAME);
				getListView().setRecyclerListener(SectionCursorTutorialActivity.this);
				setListAdapter(new SectionCursorAdatper(SectionCursorTutorialActivity.this, R.layout.section, R.layout.item, c, Test.NAME) {

					@Override
					protected void bindSectionView(View view, Context context, Cursor cursor) {
						String text = cursor.getString(cursor.getColumnIndex(SectionTest.NAME));
						TextView tv = (TextView) view.findViewById(R.id.text);
						tv.setText(text);
					}

					@Override
					protected void bindContentView(View view, Context context, Cursor cursor) {
						String text = cursor.getString(cursor.getColumnIndex(SectionTest.NAME));
						TextView tv = (TextView) view.findViewById(R.id.text);
						tv.setText(text);
					}
				});
			};

		}.execute();

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, "SectionAdapterTutorialActivity");
		menu.add(0, 1, 0, "SectionableAdapterTutorialActivity");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			startActivity(new Intent(this, SectionAdapterTutorialActivity.class));
			break;
		case 1:
			startActivity(new Intent(this, SectionableAdapterTutorialActivity.class));
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onMovedToScrapHeap(View v) {
		Log.d("TAG", "onMovedToScrapHeap()" + v);
	};

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Log.d("TAG", "onListItemClick()" + position);
	}

	private void insertData() {
		ContentResolver resolver = getContentResolver();

		ContentValues values = new ContentValues();
		values.put(SectionTest.NAME, "1");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "2");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "A");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, " left space1");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "B");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "あ");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "朝");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "ア");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "あ");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, "ア");
		resolver.insert(Test.CONTENT_URI, values);

		values.clear();
		values.put(Test.NAME, " left space2");
		resolver.insert(Test.CONTENT_URI, values);

		getListView().setCacheColorHint(Color.TRANSPARENT);
		getListView().setItemsCanFocus(true);
		getListView().setRecyclerListener(this);
	}
}